package com.ymttest.database.mongo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.YMTDateUtil;

public class CommissionWrapper {
	
	MongoDBDriver mdd = null;
	
	public CommissionWrapper(String collectionName)  {
		mdd = YmtMongoDriverFactroy.getInstance_CommissionDBdriverNew(collectionName);
	}
	
	public void saveAndUpdateRateType() {
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.commissionadmin/RateType.json");
			mdd.saveJSON(addProductBean.toString());
			
			Map<String, Object> searchMap = new HashMap<String, Object>();
			Map<String, Object> updateMap = new HashMap<String, Object>();
			
			searchMap.put("_id", addProductBean.getString("_id"));
			updateMap.put("createTime", new Date());
			updateMap.put("lastUpdateTime", new Date());
			
			BasicDBObject queryObject = new BasicDBObject(searchMap);
			BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
			mdd.getCollection().update(queryObject, updateObject, false, true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveAndUpdateDimensionCombo() {
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.commissionadmin/DimensionCombo.json");
			mdd.saveJSON(addProductBean.toString());
			
			Map<String, Object> searchMap = new HashMap<String, Object>();
			Map<String, Object> updateMap = new HashMap<String, Object>();
			
			searchMap.put("_id", addProductBean.getString("_id"));
			updateMap.put("createTime", new Date());
			updateMap.put("lastUpdateTime", new Date());
			
			BasicDBObject queryObject = new BasicDBObject(searchMap);
			BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
			mdd.getCollection().update(queryObject, updateObject, false, true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveAndUpdateSellerRateType(String beginTime, String endTime) {
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.commissionadmin/SellerRateType.json");
			mdd.saveJSON(addProductBean.toString());
			
			Map<String, Object> searchMap = new HashMap<String, Object>();
			Map<String, Object> updateMap = new HashMap<String, Object>();
			
			searchMap.put("_id", addProductBean.getString("_id"));
			updateMap.put("createTime", new Date());
			updateMap.put("lastUpdateTime", new Date());
			updateMap.put("beginTime", YMTDateUtil.parseYMDHMSDate(beginTime));
			updateMap.put("endTime", YMTDateUtil.parseYMDHMSDate(endTime));
			
			BasicDBObject queryObject = new BasicDBObject(searchMap);
			BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
			mdd.getCollection().update(queryObject, updateObject, false, true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void saveAndUpdateRateStrategy(String beginTime, String endTime) {
		try {
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.commissionadmin/RateStrategy.json");
			mdd.saveJSON(addProductBean.toString());
			
			Map<String, Object> searchMap = new HashMap<String, Object>();
			Map<String, Object> updateMap = new HashMap<String, Object>();
			
			searchMap.put("_id", addProductBean.getString("_id"));
			updateMap.put("createTime", new Date());
			updateMap.put("lastUpdateTime", new Date());
			updateMap.put("beginTime", YMTDateUtil.parseYMDHMSDate(beginTime));
			updateMap.put("endTime", YMTDateUtil.parseYMDHMSDate(endTime));
			
			BasicDBObject queryObject = new BasicDBObject(searchMap);
			BasicDBObject updateObject = new BasicDBObject("$set", updateMap);
			mdd.getCollection().update(queryObject, updateObject, false, true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("rawtypes")
	public String getSellerRateType(long sellerId, String paidTime) {
		try {
			String rateTypeId = "";
			Date beginTime = new Date();
			Date endTime = new Date();
			ArrayList<Map> list = mdd.selectPartLimitToMap("{\"sellerId\":" + sellerId +"}", 1);
			
			if (list.size() == 0){	
				return "";
			} else {
				rateTypeId = (String) list.get(0).get("rateTypeId");
				beginTime = (Date) list.get(0).get("beginTime");
				endTime = (Date) list.get(0).get("endTime");
				
				boolean isBelong = YMTDateUtil.
						belongCalendar(YMTDateUtil.parseYMDHMSDate(paidTime), beginTime, endTime);
				
				if (isBelong)
					return rateTypeId;
				else
					return "";			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
	
	@SuppressWarnings("rawtypes")
	public Map<String, Object> getRateStrategy(String rateTypeId, String paidTime, BigDecimal price) {
		try {
			Date beginTime = new Date();
			Date endTime = new Date();
			ArrayList<Map> list = mdd.selectPartLimitToMap("{\"rateTypeId\":\"" + rateTypeId +"\"}", 1);
			Map<String, Object> rateStrategy = new HashMap<String, Object>();
			
			if (list.size() == 0){	
				return null;
			} else {
				beginTime = (Date) list.get(0).get("beginTime");
				endTime = (Date) list.get(0).get("endTime");
				
				boolean isBelong = YMTDateUtil.
						belongCalendar(YMTDateUtil.parseYMDHMSDate(paidTime), beginTime, endTime);
				
				if (isBelong) {
					
					BasicDBList priceRange = (BasicDBList) list.get(0).get("priceRange");
					
					for (int i = 0; i < priceRange.size(); i++) {
						
						BasicDBObject o = (BasicDBObject) priceRange.get(i);
						
						int a = price.compareTo(new BigDecimal(o.getString("minPrice")));
						int b = price.compareTo(new BigDecimal(o.getString("maxPrice")));
						
						if (a >= 0 && b <= 0) {
							rateStrategy.put("rate", new BigDecimal(o.getString("rate")));
							break;
						}
					}
					
					rateStrategy.put("minCommissionAmount", new BigDecimal(
							(String) list.get(0).get("minCommissionAmount")));
					rateStrategy.put("maxCommissionAmount",  new BigDecimal(
							(String) list.get(0).get("maxCommissionAmount")));
					rateStrategy.put("rateTypeId", (String) list.get(0).get("rateTypeId"));
					
					return rateStrategy;
				}
				else
					return null;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) throws Exception {
		CommissionWrapper wrapper = new CommissionWrapper("RateStrategy");
		wrapper.getRateStrategy("autotest0010", "2018-03-22 14:13:17", new BigDecimal(53));
	}
}
